/*
 * Forum Pojo Class
 * 
 * This is the forum object as it is stored in the database, @tags added for synchronization with database fields.
 */

package airport.forum.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OrderBy;
import javax.persistence.OneToMany;

import airport.forum.pojo.Thread;

import java.util.Set;
import java.util.HashSet;
//forum
@Entity
@Table(name="forum")
public class Forum implements Serializable {
	//test push
	private static final long serialVersionUID = 1L;
	
	private String forumName;
	private Long forumId;
	private Long threadCount;
	
	/**
	 * Comment: specify one to many mapping here
	 */
	
	private Set<Thread> threadlist = new HashSet<Thread>(); //one to many relation:one forum may have several threads
	@OneToMany(targetEntity = Thread.class, mappedBy="forum",cascade = CascadeType.ALL, fetch = FetchType.LAZY) //'forum' is an member variable in POJO thread
	@OrderBy (value = "threadId DESC")
	public Set<Thread> getThreadList() {
		return this.threadlist;
	}
	public void setThreadList(Set<Thread> threadlist) {
		this.threadlist = threadlist;
	}
	public void addThread (Thread thread) {
		/**
		 * Comment: add thread to the list and increase the threadCount by one
		 */
		System.out.println("***<Forum POJO>:addThread***");
		System.out.println("size:"+this.threadlist.size());
		this.threadlist.add(thread);
		thread.setForum(this);
		this.threadCount = this.threadCount + 1;
		System.out.println("---<Forum POJO>:addThread---");
	}
	

	@Column(name="forum_name")
	public String getForumName() {
		return forumName;
	}
	public void setForumName(String forumName) {
		this.forumName = forumName;
	}
	
	@Column(name="thread_count")
	public Long getThreadCount() {
		return threadCount;
	}
	public void setThreadCount(Long threadCount) {
		this.threadCount = threadCount;
	}
	
	@Id
	@Column(name="forum_id")
	public Long getForumId() {
		return forumId;
	}
	public void setForumId(Long forumId) {
		this.forumId = forumId;
	}

}
